package com.acxy.ants.common.enums;

import com.acxy.ants.core.annotation.Source;
import com.acxy.ants.core.config.Plugins;
import com.acxy.ants.core.plugin.db.C3p0Plugin;
import com.acxy.ants.core.plugin.db.DbcpPlugin;
import com.acxy.ants.core.plugin.db.DruidPlugin;
import com.acxy.ants.core.plugin.db.HikariCpPlugin;

import javax.sql.DataSource;

/**
 * @author MrShun
 * @version 1.0
 * @Date 2017-05-17
 */
public enum DataSourceType {

    C3P0,
    DBCP,
    HIKARICP,
    DRUID;

    public static <T> DataSource getDataSource(Source source) {
        DataSource dataSource = null;
        if (source.value() == DataSourceType.HIKARICP) {
            HikariCpPlugin classObject = Plugins.getClassObject(HikariCpPlugin.class, source.name());
            if (classObject == null) {

            } else {
                dataSource = Plugins.getClassObject(HikariCpPlugin.class).getDataSource();
            }
        } else if (source.value() == DataSourceType.C3P0) {
            C3p0Plugin classObject = Plugins.getClassObject(C3p0Plugin.class, source.name());
            if (classObject == null) {

            } else {
                dataSource = classObject.getDataSource();
            }
        } else if (source.value() == DataSourceType.DBCP) {
            DbcpPlugin classObject = Plugins.getClassObject(DbcpPlugin.class, source.name());
            if (classObject == null) {

            } else {
                dataSource = classObject.getDataSource();
            }
        } else if (source.value() == DataSourceType.DRUID) {
            DruidPlugin classObject = Plugins.getClassObject(DruidPlugin.class, source.name());
            if (classObject == null) {

            } else {
                dataSource = classObject.getDataSource();
            }
        }
        return dataSource;
    }
}
